import 'dart:io';

import 'package:flutter/services.dart';

class KmxyBase {
  static const MethodChannel _channel = MethodChannel('kmxy_base');

  /// 打电话
  /// <uses-permission android:name="android.permission.CALL_PHONE" />
  /// Permission.phone
  static Future<String> callPhone(String phoneNum) async {
    final String? data = await _channel.invokeMethod('callPhone', phoneNum);
    return data ?? '';
  }

  /// 忽略电池优化
  /// <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
  /// 无需动态权限
  static Future<String> requestIgnoreBatteryOptimizations() async {
    final String? data =
        await _channel.invokeMethod('requestIgnoreBatteryOptimizations');
    return data ?? '';
  }

  /// 判断是否忽略电池优化
  /// <uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
  /// 无需动态权限
  static Future<bool> isIgnoringBatteryOptimizations() async {
    final bool data =
        await _channel.invokeMethod('isIgnoringBatteryOptimizations');
    return data;
  }

  /// 设置开启悬浮窗
  /// <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  /// 无需动态权限
  static Future<String> setSystemAlertWindow() async {
    final String? data = await _channel.invokeMethod('setSystemAlertWindow');
    return data ?? '';
  }

  /// 判断悬浮窗是否开启
  /// <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
  /// 无需动态权限
  static Future<bool> isSystemAlertWindow() async {
    final bool data = await _channel.invokeMethod('isSystemAlertWindow');
    return data;
  }

  /// 开始定位服务
  /// isLocationAMap = false 监听 GpslocationServiceCallback
  static Future<String> startLocationService(
      {bool isLocationAMap = true}) async {
    final String? data =
        await _channel.invokeMethod('startLocationService', isLocationAMap);
    return data ?? '';
  }

  /// 停止定位服务
  /// isLocationAMap = false 监听 GpslocationServiceCallback
  static Future<String> stopLocationService() async {
    final String? data = await _channel.invokeMethod('stopLocationService');
    return data ?? '';
  }

  /// 蒲公英地址获取-Android
  static Future<String> pgyerUrl() async {
    if (Platform.isIOS) return '';
    final String? data = await _channel.invokeMethod('pgyerUrl');
    return data ?? '';
  }

  /// 判断应用程序是否安装-Android
  static Future<bool> isAvilible(String packageName) async {
    if (Platform.isIOS) return false;
    final bool data = await _channel.invokeMethod('isAvilible', packageName);
    return data;
  }

  /// 打开wps-Android
  /// 需要在 AndroidManifest.xml 中添加 fileprovider
  static Future<String> openWps(String filePath) async {
    if (Platform.isIOS) return '';
    final String? data = await _channel.invokeMethod('openWps', filePath);
    return data ?? '';
  }

  /// 安装apk-Android
  /// 需要在 AndroidManifest.xml 中添加 fileprovider
  static Future<String> installApk(String apkPath) async {
    if (Platform.isIOS) return '';
    final String? data = await _channel.invokeMethod('installApk', apkPath);
    return data ?? '';
  }

  ///  判断CPU是否是arm64-v8a
  static Future<bool> isArm64V8a() async {
    if (Platform.isIOS) return false;
    final bool? data = await _channel.invokeMethod('isArm64V8a');
    return data ?? false;
  }

  ///  读取APK文件的版本号
  static Future<String> getApkVersionName(String apkPath) async {
    if (Platform.isIOS) return '';
    final String? data =
        await _channel.invokeMethod('getApkVersionName', apkPath);
    return data ?? '';
  }

  static setMethodCallHandler(Future<dynamic> Function(MethodCall)? handler) {
    _channel.setMethodCallHandler(handler);
  }
}
